package com.uuah.server.message;

import com.uuah.server.api.IUuahEventContext;
import com.uuah.server.api.IUuahMessage;
import com.uuah.server.api.endpoint.IEndpointURI;
import com.uuah.server.transport.RequestContextHelper;

import java.util.Date;
import java.util.Iterator;

/**
 * <code>ExceptionMessage</code> is used by the DefaultServiceExceptionStrategy
 * for wrapping an exception with a message to send via an endpointUri.
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午02:02:23
 * @version 1.0.0
 */
public class ExceptionMessage extends BaseMessage {
	private static final long serialVersionUID = -538516243574950621L;

	private Throwable exception;
	private String componentName;
	private IEndpointURI endpointUri;
	private Date timeStamp;

	public ExceptionMessage(Object message, Throwable exception,
			String componentName, IEndpointURI endpointUri) {
		super(message);
		this.exception = exception;
		timeStamp = new Date();
		this.componentName = componentName;
		this.endpointUri = endpointUri;

		IUuahEventContext ctx = RequestContextHelper.getEventContext();
		if (ctx != null) {
			IUuahMessage msg = ctx.getMessage();
			for (Iterator iterator = msg.getPropertyNames().iterator(); iterator
					.hasNext();) {
				String propertyKey = (String) iterator.next();
				setProperty(propertyKey, msg.getProperty(propertyKey));
			}
		}
	}

	public String getComponentName() {
		return componentName;
	}

	public IEndpointURI getEndpoint() {
		return endpointUri;
	}

	public Date getTimeStamp() {
		return timeStamp;
	}

	public Throwable getException() {
		return exception;
	}

	public String toString() {
		return "ExceptionMessage{" + "message=" + message + ", context="
				+ context + "exception=" + exception + ", componentName='"
				+ componentName + "'" + ", endpointUri=" + endpointUri
				+ ", timeStamp=" + timeStamp + "}";
	}
}
